package com.example.administrator.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ListView;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

public class OrderDetial  extends Activity {
    private TextView StoreName,total_price,pay_mode,pay_mode_price,order_sn,order_time;
    private Handler mHandler;
    private ListView mListView;
    private OrderDetialListAdapter adapter;
    private ListAdapter listAdapter;



    public void HttpContects(){
        SharedPreferences sharedPreferences=getSharedPreferences("data",0);
        final String access_token= sharedPreferences.getString("access_token",null);
        new Thread(new Runnable() {
            @Override
            public void run() {
               HttpContect httpContect=new HttpContect();
                String route="/api/order/detail";
                Intent intent=getIntent();
                int getid=intent.getIntExtra("id",0);
                String data="access_token="+access_token+"&order_id="+getid;

                String JsonResolutioValue=httpContect.send(route,data);
                Message message=new Message();
                message.obj=JsonResolutioValue;
                Log.i("uuuu","+++++++++++++++____________+++"+JsonResolutioValue);
                mHandler.sendMessage(message);




            }
        }).start();


    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.orderdetial);
        initview();

        HttpContects();
        adapter=new OrderDetialListAdapter(OrderDetial.this);
        mListView.setAdapter(adapter);

        mHandler=new Handler(){


            @Override
            public void handleMessage(Message msg) {
                String JsonResolutioValue= (String) msg.obj;
                super.handleMessage(msg);
                JsonResolutio(JsonResolutioValue);
                Log.i("13324234234234234","-----------"+JsonResolutioValue);
            }
        };
    }
    public void  JsonResolutio(String JsonResolutioValue ){
        try {
            JSONObject jsonObject=new JSONObject(JsonResolutioValue);
            JSONObject data=jsonObject.getJSONObject("data");
            String store_name=data.getString("store_name");
            this.StoreName.setText(store_name);
            int total_price=data.getInt("total_price");
            this.total_price.setText("¥"+total_price);
            int actual_price=data.getInt("actual_price");
            this.pay_mode_price.setText("¥"+actual_price);
            String order_sn=data.getString("order_sn");
            this.order_sn.setText(order_sn);
            String created_at=data.getString("created_at");
            listAdapter=new ListAdapter(this);

            this.order_time.setText( listAdapter.ToTime(Integer.parseInt(created_at)));

            int pay_type=data.getInt("pay_type");
            if (pay_type==0){
                this.pay_mode.setText("现金支付");

            }
            JSONArray jsonArray=data.getJSONArray("list");
            Log.i("655","*(&&&&&&&&&&&&*^^^^^^^^^"+jsonArray.length());
            ArrayList<OrderList> mList=new ArrayList<>();
            for (int i=0;i<=jsonArray.length()-1;i++){
                    OrderList item=new OrderList();
                    JSONObject jsonObject1= (JSONObject) jsonArray.get(i);
                    String sku_name=jsonObject1.getString("sku_name");
                    int quantity=jsonObject1.getInt("quantity");
                    String cup_type=jsonObject1.getString("cup_type");
                    int total_price1=jsonObject1.getInt("total_price");
                    item.setSku_name(sku_name);
                    item.setQuantity(quantity);
                    item.setCup_type(cup_type);
                    item.setTotal_price1(total_price1);

                    mList.add(item);


            }

            adapter.setData(mList);


        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    private void initview() {
        StoreName=findViewById(R.id.store_name);
        mListView=findViewById(R.id.iv1);
        pay_mode=findViewById(R.id.pay_mode);
        pay_mode_price=findViewById(R.id.pay_mode_price);
        total_price=findViewById(R.id.total_price);
        order_sn=findViewById(R.id.order_sn);
        order_time=findViewById(R.id.order_time);

    }
}
